package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateLines
//@description: 创建Lines记录
//@param: lines model.Lines
//@return: err error

func CreateLines(lines model.Lines) (err error) {
	err = global.GVA_DB.Create(&lines).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteLines
//@description: 删除Lines记录
//@param: lines model.Lines
//@return: err error

func DeleteLines(lines model.Lines) (err error) {
	err = global.GVA_DB.Delete(&lines).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteLinesByIds
//@description: 批量删除Lines记录
//@param: ids request.IdsReq
//@return: err error

func DeleteLinesByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.Lines{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateLines
//@description: 更新Lines记录
//@param: lines *model.Lines
//@return: err error

func UpdateLines(lines model.Lines) (err error) {
	err = global.GVA_DB.Save(&lines).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetLines
//@description: 根据id获取Lines记录
//@param: id uint
//@return: err error, lines model.Lines

func GetLines(id uint) (err error, lines model.Lines) {
	err = global.GVA_DB.Where("id = ?", id).First(&lines).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetLinesInfoList
//@description: 分页获取Lines记录
//@param: info request.LinesSearch
//@return: err error, list interface{}, total int64

func GetLinesInfoList(info request.LinesSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.Lines{})
    var liness []model.Lines
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.LineName != "" {
        db = db.Where("`line_name` LIKE ?","%"+ info.LineName+"%")
    }
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Preload("Tails").Find(&liness).Error
	return err, liness, total
}

